/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    can.c
  * @brief   This file provides code for the configuration
  *          of the CAN instances.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "can.h"

/* USER CODE BEGIN 0 */
#include "usart.h"
#include <stdio.h>

																			 
/* USER CODE END 0 */

CAN_HandleTypeDef hcan;

/* CAN init function */
void MX_CAN_Init(void)
{

  /* USER CODE BEGIN CAN_Init 0 */

  /* USER CODE END CAN_Init 0 */

  /* USER CODE BEGIN CAN_Init 1 */

  /* USER CODE END CAN_Init 1 */
  hcan.Instance = CAN1;
  hcan.Init.Prescaler = 4;
  hcan.Init.Mode = CAN_MODE_LOOPBACK;
  hcan.Init.SyncJumpWidth = CAN_SJW_2TQ;
  hcan.Init.TimeSeg1 = CAN_BS1_5TQ;
  hcan.Init.TimeSeg2 = CAN_BS2_3TQ;
  hcan.Init.TimeTriggeredMode = DISABLE;
  hcan.Init.AutoBusOff = ENABLE;
  hcan.Init.AutoWakeUp = ENABLE;
  hcan.Init.AutoRetransmission = DISABLE;
  hcan.Init.ReceiveFifoLocked = DISABLE;
  hcan.Init.TransmitFifoPriority = DISABLE;
  if (HAL_CAN_Init(&hcan) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN CAN_Init 2 */
  CAN_Filter_Config();	
  CAN_Start_Init();	
  /* USER CODE END CAN_Init 2 */

}

void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(canHandle->Instance==CAN1)
  {
  /* USER CODE BEGIN CAN1_MspInit 0 */

  /* USER CODE END CAN1_MspInit 0 */
    /* CAN1 clock enable */
    __HAL_RCC_CAN1_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**CAN GPIO Configuration
    PA11     ------> CAN_RX
    PA12     ------> CAN_TX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_11;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_12;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* CAN1 interrupt Init */
    HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
    HAL_NVIC_SetPriority(CAN1_RX1_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(CAN1_RX1_IRQn);
  /* USER CODE BEGIN CAN1_MspInit 1 */

  /* USER CODE END CAN1_MspInit 1 */
  }
}

void HAL_CAN_MspDeInit(CAN_HandleTypeDef* canHandle)
{

  if(canHandle->Instance==CAN1)
  {
  /* USER CODE BEGIN CAN1_MspDeInit 0 */

  /* USER CODE END CAN1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_CAN1_CLK_DISABLE();

    /**CAN GPIO Configuration
    PA11     ------> CAN_RX
    PA12     ------> CAN_TX
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);

    /* CAN1 interrupt Deinit */
    HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
    HAL_NVIC_DisableIRQ(CAN1_RX1_IRQn);
  /* USER CODE BEGIN CAN1_MspDeInit 1 */

  /* USER CODE END CAN1_MspDeInit 1 */
  }
}

/* USER CODE BEGIN 1 */
static void CAN_Filter_Config(void)
{
	CAN_FilterTypeDef  CAN_FilterInitStructure;

	CAN_FilterInitStructure.FilterBank=0;						
	CAN_FilterInitStructure.FilterMode=CAN_FILTERMODE_IDMASK;	
	CAN_FilterInitStructure.FilterScale=CAN_FILTERSCALE_32BIT;	
	
	CAN_FilterInitStructure.FilterIdHigh= ((((uint32_t)0x1314<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF0000)>>16;		
	CAN_FilterInitStructure.FilterIdLow= (((uint32_t)0x1314<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF; 
	CAN_FilterInitStructure.FilterMaskIdHigh= 0xFFFF;			
	CAN_FilterInitStructure.FilterMaskIdLow= 0xFFFF;			
	CAN_FilterInitStructure.FilterFIFOAssignment=CAN_FILTER_FIFO0;	
	CAN_FilterInitStructure.FilterActivation=ENABLE;	
	CAN_FilterInitStructure.SlaveStartFilterBank =0;
	if(HAL_CAN_ConfigFilter(&hcan,&CAN_FilterInitStructure)!=HAL_OK)
	{
		  Error_Handler();
	}
}

void CAN_Start_Init(void)
{
	if(HAL_CAN_Start(&hcan)!= HAL_OK)
	{
		  Error_Handler();		
	}
	if(HAL_CAN_ActivateNotification(&hcan,CAN_IT_RX_FIFO0_MSG_PENDING)!= HAL_OK)
	{
		  Error_Handler();		
		
	}
}

void Init_RxMes(void)
{
	CAN_RxHeaderTypeDef  CAN_RXInitStructure;

  CAN_RXInitStructure.StdId = 0x00;
  CAN_RXInitStructure.ExtId = 0x00;
  CAN_RXInitStructure.IDE = CAN_ID_STD;
  CAN_RXInitStructure.DLC = 0;
  CAN_RXInitStructure.FilterMatchIndex = 0;
}

void CAN_Test(void)
{	  
	uint32_t TxMailbox;
	uint8_t TX_Message[8] = {1,2,3,4,5,6,7,8};
	CAN_TxHeaderTypeDef  CAN_TXInitStructure;

  CAN_TXInitStructure.StdId=0x00;						 
  CAN_TXInitStructure.ExtId=0x1314;			
  CAN_TXInitStructure.IDE=CAN_ID_EXT;		
  CAN_TXInitStructure.RTR=CAN_RTR_DATA;	
  CAN_TXInitStructure.DLC=8;							
	
	if(HAL_CAN_AddTxMessage (&hcan ,&CAN_TXInitStructure,TX_Message,&TxMailbox)!= HAL_OK )
	{	
		  Error_Handler();		
	}
//	HAL_Delay (2000);

}
extern __IO uint32_t flag ;
uint8_t RX_Meassage[8]={0};
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
	CAN_RxHeaderTypeDef  CAN_RXInitStructure;
      if(HAL_CAN_GetRxMessage (hcan ,CAN_RX_FIFO0 ,&CAN_RXInitStructure,RX_Meassage)==HAL_OK)
			{
				printf("净含量,CAN收到数据啦！ \r\n");
				for(uint8_t i=0;i<8;i++)
				{
					printf ("RX_Message[%d]:%d \r\n",i,RX_Meassage[i]);
				}
				printf("************* \r\n");
			}

	}

/* USER CODE END 1 */
